﻿Imports System.IO
Imports System.Net

Public Class FTPClient

    Dim Server As String
    Dim User As String
    Dim Password As String

    Sub New(ByVal Server As String, ByVal User As String, ByVal Password As String)
        Me.Server = Server
        Me.User = User
        Me.Password = Password
    End Sub

    Function FileExists(ByVal Path As String) As Boolean
        Dim Request As FtpWebRequest
        Path = Server & "/" & Path
        Dim Pos As Integer = InStrRev(Path, "/")
        Request = WebRequest.Create(Mid(Path, 1, Pos - 1))
        Request.Credentials = New NetworkCredential(User, Password)
        Request.Proxy = Nothing
        Request.UseBinary = False
        Request.Method = WebRequestMethods.Ftp.ListDirectory
        Dim Response As FtpWebResponse = Request.GetResponse()
        Dim Reader As New StreamReader(Response.GetResponseStream(), System.Text.Encoding.Default)
        Dim Text As String = Reader.ReadToEnd
        Reader.Close()
        Response.Close()
        Return InStr(Chr(13) & LCase(Text), LCase(Mid(Path, Pos + 1)) & Chr(13)) > 0
    End Function

    Function GetFile(ByVal Path As String) As String
        Dim Request As FtpWebRequest
        Path = Server & "/" & Path
        Request = WebRequest.Create(Path)
        Request.Credentials = New NetworkCredential(User, Password)
        Request.Proxy = Nothing
        Request.UseBinary = False
        Request.Method = WebRequestMethods.Ftp.DownloadFile
        Dim Response As FtpWebResponse = Request.GetResponse()
        Dim Reader As New StreamReader(Response.GetResponseStream(), System.Text.Encoding.Default)
        Dim Text As String = Reader.ReadToEnd
        Reader.Close()
        Response.Close()
        Return Text
    End Function

End Class
